package com.payment.web.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.payment.common.annotation.Log;
import com.payment.common.base.AjaxResult;
import com.payment.common.constant.Constants;
import com.payment.common.enums.BusinessType;
import com.payment.common.page.TableDataInfo;
import com.payment.common.utils.aliyun.OSSClientUtil;
import com.payment.common.utils.file.FileUtils;
import com.payment.framework.util.ShiroUtils;
import com.payment.framework.web.base.BaseController;
import com.payment.system.domain.ResourcesData;
import com.payment.system.service.IResourcesDataService;

/**
 * 资源数据 信息操作处理
 *
 * @author payment
 * @date 2019-01-24
 */
@Controller
@RequestMapping("/system/resourcesData")
public class ResourcesDataController extends BaseController {
    private String prefix = "system/resourcesType/resourcesData";

    @Autowired
    private IResourcesDataService resourcesDataService;

    @GetMapping()
    public String resourcesData() {
        return prefix + "/resourcesData";
    }

    /**
     * 查询资源数据列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ResourcesData resourcesData) {
        startPage();
        List<ResourcesData> list = resourcesDataService.selectResourcesDataList(resourcesData);
        return getDataTable(list);
    }


    /**
     * 新增资源数据
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") String resourcesType, ModelMap mmap) {
        mmap.put("resourcesType", resourcesType);
        return prefix + "/add";
    }

    /**
     * 新增保存资源数据
     */
    @Log(title = "资源数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ResourcesData resourcesData, MultipartFile file) {
        try {
            if (file != null && !file.isEmpty()) {
                if (!FileUtils.fileType(file)) {
                    return AjaxResult.error(1, "图片类型错误");
                }
                String fileUrl = OSSClientUtil.uploadImg2Oss(file, Constants.ROTATION_CHART);
                //修改图片访问路径
               // String fileUrl = FileUploadUtils.uploadAndReturnAbsolutePath(Constants.ROTATION_CHART, file);//获取轮播图的路径
                resourcesData.setUrl(fileUrl);
            }
            resourcesData.setCreateBy(ShiroUtils.getLoginName());
            return toAjax(resourcesDataService.insertResourcesData(resourcesData));
        } catch (Exception e) {

            return error("保存失败！"+e.toString());
        }
    }

    /**
     * 修改资源数据
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        ResourcesData resourcesData = resourcesDataService.selectResourcesDataById(id);
        mmap.put("resourcesData", resourcesData);
        return prefix + "/edit";
    }

    /**
     * 修改保存资源数据
     */
    @Log(title = "资源数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ResourcesData resourcesData, MultipartFile file) {
        try {
            if (file != null && !file.isEmpty()) {
                if (!FileUtils.fileType(file)) {
                    return AjaxResult.error(1, "图片类型错误");
                }
                String fileUrl = OSSClientUtil.uploadImg2Oss(file, Constants.ROTATION_CHART);
                //修改图片访问路径
                //String fileUrl = FileUploadUtils.uploadAndReturnAbsolutePath(Constants.ROTATION_CHART, file);//获取轮播图的路径
                resourcesData.setUrl(fileUrl);
            }
            resourcesData.setUpdateBy(ShiroUtils.getLoginName());
            return toAjax(resourcesDataService.updateResourcesData(resourcesData));
        } catch (Exception e) {

            return error("修改轮播图失败！");
        }


    }

    /**
     * 删除资源数据
     */
    @Log(title = "资源数据", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(resourcesDataService.deleteResourcesDataByIds(ids));
    }

}
